﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class _Tapershanktwistdrillsused
    {
        public static void Tapershanktwistdrillsused(Dictionary<string, Object> data)
        {

            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
                                                                           //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            var d = 12.0;
            try
            {
                d = Convert.ToDouble(data["dc"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[d] not found");
            }
            var l1 = 101.0;
            try
            {
                l1 = Convert.ToDouble(data["lcf"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[l1] not found");
            }
            var l = 182.0;
            try
            {
                l = Convert.ToDouble(data["oal"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[l] not found");
            }
            var msyz = 1.0;
            try
            {
                msyz = Convert.ToDouble(data["msyz"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[msyz] not found");
            }
            var d1 = 15.1;
            try
            {
                d1 = Convert.ToDouble(data["dcx"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[d1] not found");
            }
            var l2 = 1.1;
            try
            {
                l2 = Convert.ToDouble(data["l2"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[l2] not found");
            }
            var l0 = 1.1;
            try
            {
                l0 = Convert.ToDouble(data["pl"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[l0] not found");
            }
            //坐标

            var v1 = new Vector2(0, 0);
            var v2 = fun.Polar(v1, 90 * pi / 180, 9.88 / 20 * d);
            var v3 = fun.Polar(v2, pi, 10.01 / 100 * l1);
            var v4 = fun.Polar(v1, -90 * pi / 180, 9.88 / 20 * d);
            var v5 = fun.Polar(v4, pi, 29.92 / 223.94 * l1);
            var v6 = new Vector2(v1.X - 41.91 / 223.94 * l1, v1.Y + 9.28 / 20 * d);
            var v7 = new Vector2(v1.X - 7.8 / 223.94 * l1, v1.Y + 4.16 / 20 * d);
            var v8 = new Vector2(v1.X - 7.34 / 223.94 * l1, v1.Y);
            var v9 = new Vector2(v1.X - 9.87 / 223.94 * l1, v1.Y - 1.5 / 20 * d);
            var v10 = new Vector2(v1.X - 16.53 / 223.94 * l1, v1.Y);
            var v11 = new Vector2(v1.X - 20.32 / 223.94 * l1, v1.Y + 1.42 / 20 * d);
            var v12 = new Vector2(v1.X - 59.82 / 223.94 * l1, v1.Y + 11.31 / 20 * d);
            var v13 = new Vector2(v1.X - 35.03 / 223.94 * l1, v1.Y - 8.62 / 20 * d);
            var v14 = new Vector2(v1.X - 52.22 / 223.94 * l1, v1.Y);
            var v15 = new Vector2(v1.X - 78.5 / 223.94 * l1, v1.Y + 10.01 / 20 * d);
            var v16 = new Vector2(v1.X - 104.58 / 223.94 * l1, v1.Y + 9.58 / 20 * d);
            var v17 = new Vector2(v1.X - 33.39 / 223.94 * l1, v1.Y);
            var v18 = new Vector2(v1.X - 30 / 223.94 * l1, v1.Y - 5.09 / 20 * d);
            if (l1 < 70)
            {
                v18 = new Vector2(v1.X - 28 / 223.94 * l1, v1.Y - 5.09 / 20 * d);
            }



            var polyline1 = Pus.AddPointsToLwPolyLine(v3, v2, v4, v5);
            var arc1 = fun.DrawArc3(v7, v3, v6);
            var arc2 = fun.DrawArc3(v9, v8, v7);
            var arc3 = fun.DrawArc3(v11, v10, v9);
            var arc4 = fun.DrawArc3(v11, v6, v12);
            var arc5 = fun.DrawArc3(v17, v12, v15);
            var arc6 = fun.DrawArc3(v14, v15, v16);
            var arc7 = fun.DrawArc3(v14, v13, v5);
            var v19 = fun.Polar(new Vector2(arc7.Center.X, arc7.Center.Y), 252 * pi / 180, arc7.Radius);
            if (l1 > 70)
            {
                var arc8 = fun.DrawArc3(v5, v18, v17);
                fun.drawBArc(dxf, arc8, 0.2, contour);
            }
            if (l1 < 70)
            {
                var arc8 = fun.DrawArc3(v5, v18, v17);
                fun.drawBArc(dxf, arc8, 0.2, contour);
            }
            var v20 = new Vector2(v1.X - 131.04 / 223.94 * l1, v1.Y + 9.93 / 20 * d);
            var v21 = new Vector2(v1.X - 80.02 / 223.94 * l1, v1.Y);
            var v22 = new Vector2(v1.X - 58.17 / 223.94 * l1, v1.Y - 7.72 / 20 * d);
            var arc9 = fun.DrawArc3(v21, v22, v13);
            var v23 = fun.Polar(new Vector2(arc9.Center.X, arc9.Center.Y), 271 * pi / 180, arc9.Radius);
            var v24 = new Vector2(v1.X - 59.57 / 223.94 * l1, v1.Y - 9.5 / 20 * d);
            var v25 = new Vector2(v1.X - 83.82 / 223.94 * l1, v1.Y);
            var v27 = new Vector2(v1.X - 90.52 / 223.94 * l1, v1.Y + 4.47 / 20 * d);
            var v28 = new Vector2(v1.X - 88.08 / 223.94 * l1, v1.Y - 7.62 / 20 * d);
            var v29 = new Vector2(v1.X - 106.73 / 223.94 * l1, v1.Y);
            var v30 = new Vector2(v1.X - 157.25 / 223.94 * l1, v1.Y + 8.25 / 20 * d);
            var v31 = new Vector2(v1.X - 113.31 / 223.94 * l1, v1.Y - 9.56 / 20 * d);
            var v32 = new Vector2(v1.X - 138.61 / 223.94 * l1, v1.Y);
            var v33 = new Vector2(v1.X - 165.62 / 223.94 * l1, v1.Y + 9.42 / 20 * d);
            var v34 = new Vector2(v1.X - 145.54 / 223.94 * l1, v1.Y);
            var v35 = new Vector2(v1.X - 128.78 / 223.94 * l1, v1.Y - 5.3 / 20 * d);
            var v36 = new Vector2(v1.X - 145.63 / 223.94 * l1, v1.Y - 7.9 / 20 * d);
            var v37 = new Vector2(v1.X - 164.57 / 223.94 * l1, v1.Y);
            var v38 = new Vector2(v1.X - 187.8 / 223.94 * l1, v1.Y + 10.01 / 20 * d);
            var v39 = new Vector2(v1.X - 209.47 / 223.94 * l1, v1.Y + 9.5 / 20 * d);
            var v40 = new Vector2(v1.X - 189.68 / 223.94 * l1, v1.Y);
            var v41 = new Vector2(v1.X - 164.11 / 223.94 * l1, v1.Y - 8.04 / 20 * d);
            var v42 = new Vector2(v1.X - 198.69 / 223.94 * l1, v1.Y - 6.47 / 20 * d);
            var v43 = new Vector2(v1.X - 214.2 / 223.94 * l1, v1.Y);
            var v45 = new Vector2(v1.X - 194.81 / 223.94 * l1, v1.Y);
            var v46 = new Vector2(v1.X - 224.62 / 223.94 * l1, v1.Y + 11.64 / 20 * d);
            var v47 = new Vector2(v1.X - 218.69 / 223.94 * l1, v1.Y + 2.76 / 20 * d);
            var v48 = new Vector2(v1.X - 220.3 / 223.94 * l1, v1.Y + 8.07 / 20 * d);
            var v49 = new Vector2(v1.X - 230.6 / 223.94 * l1, v1.Y);
            var v50 = new Vector2(v1.X - 224.94 / 223.94 * l1, v1.Y - 7.82 / 20 * d);
            var v51 = new Vector2(v1.X - 215.73 / 223.94 * l1, v1.Y - 8.37 / 20 * d);
            var v52 = new Vector2(v1.X - 223.29 / 223.94 * l1, v1.Y + 4.03 / 20 * d);
            Vector2 v53 = new Vector2(v25.X, v1.Y + d / 2 + 2);
            Vector2 v54 = new Vector2(v25.X, v1.Y - d / 2 - 2);



            var arc10 = fun.DrawArc3(v21, v16, v20);
            var v26 = fun.Polar(new Vector2(arc10.Center.X, arc10.Center.Y), 78 * pi / 180, arc10.Radius);
            var arc11 = fun.DrawArc3(v25, v27, v26);
            var arc12 = fun.DrawArc3(v25, v24, v23);
            var arc13 = fun.DrawArc3(v29, v28, v24);
            var arc14 = fun.DrawArc3(v29, v20, v30);
            var arc15 = fun.DrawArc3(v32, v30, v33);
            var arc16 = fun.DrawArc3(v34, v31, v28);
            var arc17 = fun.DrawArc3(v32, v35, v31);
            var arc18 = fun.DrawArc3(v34, v33, v38);
            var arc19 = fun.DrawArc3(v37, v38, v39);
            var arc20 = fun.DrawArc3(v37, v36, v31);
            var arc21 = fun.DrawArc3(v40, v41, v36);
            var arc22 = fun.DrawArc3(v43, v42, v41);
            var v44 = fun.Polar(new Vector2(arc22.Center.X, arc22.Center.Y), 272 * pi / 180, arc22.Radius);
            var arc23 = fun.DrawArc3(v39, v45, v44);
            var arc24 = fun.DrawArc3(v47, v52, v49);
            var arc25 = fun.DrawArc3(v47, v48, v46);
            var arc26 = fun.DrawArc3(v50, v51, v42);
            var arc27 = fun.DrawArc3(v40, v39, v46);
            var polyline2 = Pus.AddPointsToLwPolyLine(v46, v49, v50);
            var polyline3 = Pus.AddPointsToLwPolyLine(v47, v43);

            //dxf.AddEntity(new EntityObject[] { arc1, arc2, arc3, arc4, arc5, arc6, arc7, arc8, arc9, arc10, arc11, arc12, arc13, arc14, arc15, arc16, arc17, arc18, arc19, arc20, arc21, arc22, arc23, arc24, arc25, arc26, arc27, polyline1, polyline2, polyline3 });
            

            fun.drawBArc(dxf, arc1, 0.2, contour);
            fun.drawBArc(dxf, arc2, 0.2, contour);
            fun.drawBArc(dxf, arc3, 0.2, contour);
            fun.drawBArc(dxf, arc4, 0.2, contour);
            fun.drawBArc(dxf, arc5, 0.2, contour);
            fun.drawBArc(dxf, arc6, 0.2, contour);
            fun.drawBArc(dxf, arc7, 0.2, contour);
            fun.drawBArc(dxf, arc9, 0.2, contour);
            fun.drawBArc(dxf, arc10, 0.2, contour);
            fun.drawBArc(dxf, arc11, 0.2, contour);
            fun.drawBArc(dxf, arc12, 0.2, contour);
            fun.drawBArc(dxf, arc13, 0.2, contour);
            fun.drawBArc(dxf, arc14, 0.2, contour);
            fun.drawBArc(dxf, arc15, 0.2, contour);
            fun.drawBArc(dxf, arc16, 0.2, contour);
            fun.drawBArc(dxf, arc17, 0.2, contour);
            fun.drawBArc(dxf, arc18, 0.2, contour);
            fun.drawBArc(dxf, arc19, 0.2, contour);
            fun.drawBArc(dxf, arc20, 0.2, contour);
            fun.drawBArc(dxf, arc21, 0.2, contour);
            fun.drawBArc(dxf, arc22, 0.2, contour);
            fun.drawBArc(dxf, arc23, 0.2, contour);
            fun.drawBArc(dxf, arc24, 0.2, contour);
            fun.drawBArc(dxf, arc25, 0.2, contour);
            fun.drawBArc(dxf, arc26, 0.2, contour);
            fun.drawBArc(dxf, arc27, 0.2, contour);
            fun.PolyFill(dxf, polyline1, 0.2, contour);
            fun.PolyFill(dxf, polyline2, 0.2, contour);
            fun.PolyFill(dxf, polyline3, 0.2, contour);


            Milling_cutter.Morse_tapershank(dxf, v1, d, l, 161.56 / 156.89 * l1, msyz, 0);

            var a1 = fun.Polar(v49, pi, 10);
            var a2 = fun.Polar(v49, 0, l + 10);

            Vector2 v55 = fun.Inters1(v53, v54, arc13, 2);
            Vector2 v56 = fun.Inters1(v53, v54, arc6, 1);

            Line line00 = new Line(a1, a2);
            line00.Layer = centre;
            line00.Lineweight = Lineweight.W25;
            line00.Linetype = Linetype.DashDot;
            dxf.AddEntity(line00);

            Line line1 = new Line(v54, v53);
            line1.Layer = centre;
            line1.Lineweight = Lineweight.W25;
            line1.Linetype = Linetype.DashDot;
            dxf.AddEntity(line1);

            var c1 = fun.Polar(a2, 0, 60);
            var c2 = fun.Polar(c1, pi, d);
            var c3 = fun.Polar(c1, 0, d);
            var c4 = fun.Polar(c1, 90 * pi / 180, d);
            var c5 = fun.Polar(c1, -90 * pi / 180, d);
            var c6 = fun.Polar(c1, 104 * pi / 180, d / 2);
            var c7 = fun.Polar(c1, 284 * pi / 180, d / 2);
            var c8 = fun.Polar(c1, 106 * pi / 180, 6.34 / 9.19 * d / 2);
            var c9 = fun.Polar(c1, 286 * pi / 180, 6.34 / 9.19 * d / 2);
            var c10 = fun.Polar(c1, 209 * pi / 180, 6.34 / 9.19 * d / 2);
            var c11 = fun.Polar(c1, 29 * pi / 180, 6.34 / 9.19 * d / 2);
            var c12 = fun.Polar(c1, 144 * pi / 180, 1.94 / 9.19 * d / 2);
            var c13 = fun.Polar(c1, 324 * pi / 180, 1.94 / 9.19 * d / 2);
            Line line01 = new Line(c2, c3);
            line01.Layer = centre;
            line01.Lineweight = Lineweight.W25;
            line01.Linetype = Linetype.DashDot;
            dxf.AddEntity(line01);
            Line line02 = new Line(c4, c5);
            line02.Layer = centre;
            line02.Lineweight = Lineweight.W25;
            line02.Linetype = Linetype.DashDot;
            dxf.AddEntity(line02);

            var polyline4 = Pus.AddPointsToLwPolyLine(c6, c7, c6);
            polyline4.Vertexes[0].Bulge = Math.Tan(180 * pi / 180 / 4);
            polyline4.Vertexes[1].Bulge = Math.Tan(180 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline4, 0.2, contour);
            var polyline5 = Pus.AddPointsToLwPolyLine(c8, fun.Polar(c1, -90 * pi / 180, 6.34 / 9.19 * d / 2));
            polyline5.Vertexes[0].Bulge = Math.Tan(164 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline5, 0.2, contour);
            var polyline6 = Pus.AddPointsToLwPolyLine(c9, fun.Polar(c1, 90 * pi / 180, 6.34 / 9.19 * d / 2));
            polyline6.Vertexes[0].Bulge = Math.Tan(164 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline6, 0.2, contour);

            var arc28 = fun.DrawArc3(c12, c8, c6);
            var arc29 = fun.DrawArc3(c13, c9, c7);
            fun.drawBArc(dxf, arc28, 0.2, contour);
            fun.drawBArc(dxf, arc29, 0.2, contour);
            var polyline7 = Pus.AddPointsToLwPolyLine(c12, c10);
            polyline7.Vertexes[0].Bulge = Math.Tan(-72 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline7, 0.2, contour);
            var polyline8 = Pus.AddPointsToLwPolyLine(c13, c11);
            polyline8.Vertexes[0].Bulge = Math.Tan(-72 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline8, 0.2, contour);

            


            var height = 2.5;//字体高度
            var offset = 10;//偏移量
            var arrowsize = 1;//箭头大小
            var exline = 1;//延伸线长度

            fun.dimrot1(dxf, v46, v50, 90, d * 4, 0, height, arrowsize, exline, "DC=" + d, "", "");
            fun.dimrot1(dxf, v55, v56, 90, 0, 0, height, arrowsize, exline, "DCX=" + d1, "", "");
            fun.dimrot1(dxf, v49, fun.Polar(v49, 0, l1), 180, d * 5, 0, height, arrowsize, exline, "LCF=" + l1, "", "");
            fun.dimrot1(dxf, v49, fun.Polar(v49, 0, l), 180, d * 8, 0, height, arrowsize, exline, "OAl=" + l, "", "");
            fun.dimrot1(dxf, v55, v50, 180, d * 2, 0, height, arrowsize, exline, "l2=" + l2, "", "");
            fun.dimrot1(dxf, v49, v50, 180, d * 2+(v49.Y +v50.Y )/2-(v50.Y +v55.Y )/2, 0, height, arrowsize, exline, "PL=" + l0, "", "");

            fun.jt(dxf, v38, 60, 12, height, "1:50");
            fun.jt(dxf, new Vector2 ((l-l1)/2,d/2), 60, 24, height, "莫氏圆锥柄");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
